angular.module('mamApp').controller('contractManagementController', ['$scope', '$state', '$uibModal', 'ImsDialog', 'ContractManagementManager', '$filter',
    function ($scope, $state, $uibModal, ImsDialog, ContractManagementManager, $filter) {
        $scope.total = 0;

        $scope.cpPropertyes = [
            {key: 0, value: "CP性质"},
            {key: 12, value: $scope.table[12]},
            {key: 13, value: $scope.table[13]}
        ];

        $scope.coopTypes = [
            {key: 0, value: "CP类型"},
            {key: 41, value: $scope.table[41]},
            {key: 51, value: $scope.table[51]},
            {key: 52, value: $scope.table[52]},
            {key: 53, value: $scope.table[53]},
            {key: 54, value: $scope.table[54]}
        ];

        $scope.search = {
            pageNo: $state.params.pageNo || 1,
            pageSize: $state.params.pageSize || 10,
            reviewStatus: $state.params.reviewStatus,
            coopType: $state.params.coopType ? parseInt($state.params.coopType) : 0,
            cpProperty: $state.params.cpProperty ? parseInt($state.params.cpProperty) : 0,
            startDate: $state.params.startDate || null,
            endDate: $state.params.endDate || null,
            singerDate: $state.params.singerDate || null,
            keyword: $state.params.keyword
        };
        $scope.signerDate = $scope.search.singerDate ? new Date($state.params.singerDate) : null;
        $scope.noPermission = false;


        ContractManagementManager.contractManagementList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
            if (result.data.c == "n") {
                $scope.noPermission = true;

            } else {
                $scope.noPermission = false;
                $scope.contractManagement = result.data.pageList;
                $scope.paginationConf.totalItems = result.data.total;
            }

        })

        $scope.selectChange = function () {
            $scope.search.pageNo=1;
            $state.go($state.current.name, $scope.search);
        }

        $scope.dateChange = function (singerDate) {
            $scope.search.pageNo=1;
            $scope.search.singerDate = $filter('date')(singerDate, "yyyy-MM-dd");
            console.log( $scope.search.singerDate)
            $state.go($state.current.name, $scope.search);
        }

        $scope.paginationConf = {
            currentPage: $state.params.pageNo || 1,
            itemsPerPage: $state.params.pageSize || 10,
            pagesLength: 9,
            search: 0,
            perPageOptions: [10, 20, 30, 40, 50],
            onChange: function () {
                $scope.search.pageNo = this.currentPage;
                $scope.search.pageSize = this.itemsPerPage;
                $state.go($state.current.name, $scope.search);
            }
        };
        $scope.searchClick = function (keyword) {
            $scope.search.keyword = keyword;
            $scope.search.reviewStatus = $scope.search.reviewStatus == 'null' ? null : $scope.search.reviewStatus;
            $state.go($state.current.name, $scope.search)

        }

        $scope.rangeDate = {
            startDate: $scope.search.startDate ? moment($scope.search.startDate).format('X') : null,
            endDate: $scope.search.endDate ? moment($scope.search.endDate).format('X') : null,
            onSelected: function (data, scope) {  //时间选择确认事件
                $scope.search.startDate = data.startDate ? moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD") : null;
                $scope.search.endDate = data.endDate ? moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD") : null;
                $scope.search.pageNo=1;
                $state.go($state.current.name, $scope.search);

            }
        };

        $scope.allCheckChange = function (allChecked) {

            $scope.contractManagement.forEach(function (item) {
                item.checked = allChecked;
            })
        }

        $scope.itemCheckChange = function (album) {
            $scope.allChecked = true;
            $scope.contractManagement.forEach(function (item) {
                if (!item.checked) {
                    $scope.allChecked = false;

                }
            })
        }

        $scope.deleteSelected = function () {
            var ids = [];
            $scope.contractManagement.forEach(function (contract) {
                if (contract.checked) {
                    ids.push(contract.id)
                }
            })

            if (ids.length == 0) {
                return false;
            }

            ImsDialog.open({title: "合约删除", message: "确定删除所选的所有合约吗？"}).then(function (a) {
                if (a == "ok") {
                    ContractManagementManager.contractDelete(ids).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })
        }

        $scope.delete = function (contract) {
            ImsDialog.open({title: "合约删除", message: "确定删除这条合约吗？"}).then(function (a) {
                if (a == "ok") {
                    ContractManagementManager.contractDelete([contract.id]).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })
        }


    }])

